<?php

namespace App\Http\Controllers\Admin;

use App\Models\Goods;
use App\Models\GoodsBrand;
use App\Models\Spec;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * 品牌控制器
 * @package App\Http\Controllers\Admin
 */
class GoodsBrandController extends Controller
{
	/**
	 * 品牌列表
	 * User: Clannad ~ ☆
	 * DataTime：2019/11/15 12:24
	 */
	public function index()
    {
		$GoodsBrandList = GoodsBrand::query()
				->orderBy('id','desc')
				->paginate(10);

		return view('admin.goods_brand.index',compact('GoodsBrandList'));
    }


	public function create()
    {
		return view('admin.goods_brand.create');
    }


	public function store(Request $request, GoodsBrand $GoodsBrand)
	{
		$insertData = $request->except(['_token']);

		$GoodsBrand->fill($insertData);
		$GoodsBrand->save();

		return redirect()->route('admin.goods_brand.index')->with('success','添加品牌成功');
    }


	public function edit(Request $request,$id)
	{
		$goods_brand = GoodsBrand::findOrfail($id);

		return view('admin.goods_brand.edit',compact('goods_brand'));
    }


	public function update(Request $request,$id)
	{
		$GoodsBrand = GoodsBrand::findOrFail($id);

		$updateData = $request->except(['_token']);

		$GoodsBrand->fill($updateData);
		$GoodsBrand->update();

		return redirect()->route('admin.goods_brand.index')->with('success','修改品牌成功');
    }


	public function destroy($id)
    {
    	$brand = GoodsBrand::findOrfail($id);
		//销毁品牌的时候，要判断有没有规格在用，在用的不可删除。
		$goods = Goods::query()->where('brand_id',$id)->count();
		if($goods){
		    return back()->with('danger','该品牌有商品在使用，请先删除在用的商品');
		}

		$brand->delete();

		return redirect()->route('admin.goods_brand.index')->with('success','删除成功');
    }

}
